package org.springframework.license.service;

import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @Title WindowComputerService
 * @Author zhijun.Guo
 * @Email tiankong061015@163.com
 * @Time 2024/9/2 20:50
 * @Description
 */
public class WindowComputerService extends AbstractComputerService {
    /**
     * 获取IP地址
     *
     * @return ip
     */
    @Override
    public List<String> getIp() throws Exception {

        List<InetAddress> inetAddresses = getLocalAllInetAddress();
        if(CollectionUtils.isEmpty(inetAddresses)) return null;
        return inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList());
    }

    /**
     * 获取MAC地址
     *
     * @return MAC
     */
    @Override
    public List<String> getMac() throws Exception {
        List<InetAddress> inetAddresses = getLocalAllInetAddress();
        if(CollectionUtils.isEmpty(inetAddresses)) return null;
        return inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
    }

    /**
     * 获取CPU型号
     *
     * @return CPU
     */
    @Override
    public String getCpu() throws IOException {
        //序列号
        String serialNumber = "";

        //使用WMIC获取CPU序列号
        Process process = Runtime.getRuntime().exec("wmic cpu get processorid");
        process.getOutputStream().close();
        Scanner scanner = new Scanner(process.getInputStream());

        if(scanner.hasNext()){
            scanner.next();
        }

        if(scanner.hasNext()){
            serialNumber = scanner.next().trim();
        }

        scanner.close();
        return serialNumber;

    }

    /**
     * 获取主板型号
     *
     * @return 主板型号
     */
    @Override
    public String getMain() throws IOException {
        //序列号
        String serialNumber = "";

        //使用WMIC获取主板序列号
        Process process = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
        process.getOutputStream().close();
        Scanner scanner = new Scanner(process.getInputStream());

        if(scanner.hasNext()){
            scanner.next();
        }

        if(scanner.hasNext()){
            serialNumber = scanner.next().trim();
        }

        scanner.close();
        return serialNumber;

    }
}
